<?php
/**
 * @version 0.8.3 2010-10-07
 * @package Joomla
 * @subpackage LeagueResults
 * @copyright (C)  2008, 2009, 2010 Henryk Drozd
 * @license GNU/GPL, see LICENSE.php
 * LeagueResults is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.

 * LeagueResults is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');


class lrModelTablecontent extends JModel
{
var $_id = null;
var $_data = null;
var $_tableheaders = null;

function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}
function setId($id)
	{
		$this->_id	    = $id;
		$this->_data	= null;
	}

function &getData()
	{
		if ($this->_loadData())
		{

		}
		else  $this->_initData();

		return $this->_data;
	}
function _loadData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
		
					$query = 'SELECT   
		tc.*, th.leagueseasons_id, s.name, th.tdate, th.pointsformat, t.tablename, t.tbadge
					FROM #__lr_tablecontents AS tc
					JOIN #__lr_tableheaders AS th ON th.id = tc.tableheaders_id
					JOIN #__lr_teams AS t ON tc.team_id = t.id				
					JOIN #__lr_leagueseasons AS s ON th.leagueseasons_id = s.id'
					. ' WHERE tc.id = '.$this->_id	;
		
		
			$this->_db->setQuery($query);

			$this->_data = $this->_db->loadObject();
//print_r($this->_data);aaa();
			return (boolean) $this->_data;
		}
	return true;
	}
	
	function _initData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$tablecontent = new stdClass();
			$tablecontent->id				= 0;
			$tablecontent->tableheaders_id	= null;
			$tablecontent->position			= null;
			$tablecontent->lastposition		= null;
			$tablecontent->team_id			= null;
			$tablecontent->matches			= null;
			$tablecontent->points			= null;
			$tablecontent->wins 			= null;
			$tablecontent->draws			= null;
			$tablecontent->defeats			= null;
			$tablecontent->scoreplus		= null;
			$tablecontent->scoreminus		= null;
			$tablecontent->published		= 1;
			
			$this->_data			= $tablecontent;
			return (boolean) $this->_data;
		}
		return true;
	}
	
	
	function store($data) 
	{
		$lrsettings = lrAdmin::config();
		$user		= & JFactory::getUser();
		$config 	= & JFactory::getConfig();

		$tzoffset 	= $config->getValue('config.offset');

		$row  =& $this->getTable('lr_tablecontents', '');

		if (!$row->bind($data)) {
			JError::raiseError(500, $this->_db->getErrorMsg() );
			return false;
		}

		jimport('joomla.filesystem.file');
		
		$row->id = (int) $row->id;

		$nullDate	= $this->_db->getNullDate();

		// Make sure the data is valid
		if (!$row->check($lrsettings)) {
			$this->setError($row->getError());
			return false;
		}

		// Store it in the db
		if (!$row->store()) {
			JError::raiseError(500, $this->_db->getErrorMsg() );
			return false;
		}

		return $row->id;
	}
function &getTeams()
	{
		$query = 'SELECT id AS value, name AS text'
				. ' FROM #__lr_teams'
				. ' WHERE published = 1'
				;

		$this->_db->setQuery( $query );

		$this->_teams = $this->_db->loadObjectList();
		return $this->_teams;
	}

function &getTableheaders()
	{
		$query = 'SELECT id AS value, tname AS text'
				. ' FROM #__lr_tableheaders'
				. ' WHERE published = 1'
				;

		$this->_db->setQuery( $query );

		$this->_tableheaders = $this->_db->loadObjectList();
		
		return $this->_tableheaders;
	}

	
}

?>